package org.zpmis.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.common.result.ResultCode;

import javax.validation.ConstraintViolationException;
import java.nio.file.AccessDeniedException;


@RestControllerAdvice
@Slf4j
public class ExceptionRestControllerAdvice {

    /**
     * Throwable 全局异常处理
     * @param
     * @return
     */
    @ExceptionHandler(value = Throwable.class)
    public PlatformResult throwableHandler(Throwable throwable){
        log.error("[ExceptionRestControllerAdvice]---系统异常：",throwable);
        return  PlatformResult.failure(ResultCode.ERROR);
    }

    /**
     * HttpRequestMethodNotSupportedException 全局异常处理
     * @param
     * @return
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public PlatformResult requestMethodHandler(HttpRequestMethodNotSupportedException throwable){
        log.error("[ExceptionRestControllerAdvice]---不支持的请求方式--->当前请求方式：{},异常信息:{}",throwable.getMethod(),throwable.getMessage());
        return  PlatformResult.failure(ResultCode.ERROR,throwable.getMessage());
    }

    /**
     * AccessDeniedException 权限异常的消息处理
     * @param
     * @return
     */
    @ExceptionHandler(value = AccessDeniedException.class)
    public PlatformResult businessExceptionHandler(AccessDeniedException accessDeniedException){
        log.error("[ExceptionRestControllerAdvice]---权限异常：",accessDeniedException);
        return  PlatformResult.failure(ResultCode.NO_PERMISSION,accessDeniedException.getMessage());
    }

    /**
     * BusinessException 自定义异常的消息处理
     * @param
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public PlatformResult businessExceptionHandler(BusinessException businessException){
        log.error("[ExceptionRestControllerAdvice]---自定义异常：",businessException);
        return  PlatformResult.failure(ResultCode.PARAM_ERROR,businessException.getMsg());
    }

    /**
     * ValidException  body参数校验失败时的统一返回
     * @param
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    public PlatformResult requestValidExceptionHandler(BindException bindException){
        log.error("[ExceptionRestControllerAdvice]---body参数校验失败：",bindException);
        StringBuilder msg = new StringBuilder();
        bindException.getBindingResult().getFieldErrors().forEach(c ->
                msg.append(c.getDefaultMessage()).append("&"));
        return  PlatformResult.failure(ResultCode.PARAM_ERROR,msg.substring(0, msg.length() - 1));
    }

    /**
     * MethodArgumentNotValidException  body参数校验失败时的统一返回
     * @param
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public PlatformResult requestValidExceptionHandler(MethodArgumentNotValidException bindException){
        log.error("[ExceptionRestControllerAdvice]---body参数校验失败：",bindException);
        StringBuilder msg = new StringBuilder();
        bindException.getBindingResult().getFieldErrors().forEach(c ->
                msg.append(c.getDefaultMessage()).append("&"));
        return  PlatformResult.failure(ResultCode.PARAM_ERROR,msg.substring(0, msg.length() - 1));
    }
    /**
     * 方法参数校验失败时的统一返回
     * @param e ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public PlatformResult constrainViolationParamHandler(ConstraintViolationException e) {
        log.error("[ExceptionRestControllerAdvice]---方法参数校验失败：",e);
        StringBuilder msg = new StringBuilder();
        e.getConstraintViolations().forEach(c ->
                msg.append(c.getMessage()).append(":").append(c.getInvalidValue()).append(">"));
        return PlatformResult.failure(ResultCode.PARAM_ERROR,msg.substring(0, msg.length() - 1));
    }


}
